<?php

namespace app\api\controller;


use app\api\validate\Redis as RedisValidate;
use think\Facade\Cache;

/**
 * Redis缓存控制器
 */
class RedisCache
{

    /**
     * 设置缓存
     * @param string $key
     * @param mixed $value
     * @return string
     */
    public function set(string $key, mixed $value): string
    {
        //TODO 验证器
        $validate = new RedisValidate();
        if (!$validate->scene('set')->check(input())) {
            return CustomJsonResult(400, $validate->getError());
        }
        Cache::set($key, $value);
        return CustomJsonResult(200, '设置成功');
    }

    /**
     * 获取缓存
     * @param string $key
     * @return string
     */
    public function get(string $key): string
    {
        //TODO 验证器
        $validate = new RedisValidate();
        if (!$validate->scene('get')->check(input())) {
            return CustomJsonResult(400, $validate->getError());
        }
        $result = Cache::get($key);
        return CustomJsonResult(200, '获取成功', $result);
    }
}